/*
 * QueryMainPanel.java
 *
 * Created on 8. Juni 2008, 23:38
 */

package twi.view;

import java.util.ArrayList;

import jtools.gui.CodeCompletionTextArea;
import twi.KeySearch;
import twi.OWLModel;
import twi.QueryManager;
import twi.query.template2.TemplateMainView2;
import twi.query.template2.view.TemplateListPanel;
import twi.result.QueryResult;
import twi.result.table.view.TableResultView;
import twi_project.TWI;

/**
 * 
 * @author Soeren
 */
public class QueryMainPanel extends javax.swing.JPanel {

	ResultView resultView[] = null;

	TemplatePanel tp = null;

	/** Creates new form QueryMainPanel */
	public QueryMainPanel() {
		initComponents();
		setup();
	}

	private void setup() {
		setupQuerySection();
		setupResultSection();
		TWI.getInstance().setQueryPanel(this);
	}

	private void setupResultSection() {
		ResultView[] tmp = { new PrefuseGraphViewDemo(), new TableResultView() };
		this.resultView = tmp;
		for (int i = 0; i < this.resultView.length; i++)
			this.resultPanel.add(this.resultView[i].getVisualComponent(),
					this.resultView[i].getName());

		for (int i = 0; i < this.resultView.length; i++)
			this.resultView[i].setup();
		update();
	}

	public void updateTemplates() {
		this.tp.updateTemplates();
	}

	private void setupQuerySection() {
		OWLModel model = TWI.getInstance().getOntologieModel();
		if (model != null) {
			ArrayList<String> voc = KeySearch.getCompleteVocabulary(model);
			ArrayList<String> ep = new ArrayList<String>();
			for (int i = 0; i < voc.size(); i++) {
				String n = voc.get(i);
				ep.add("+" + n);
				ep.add("-" + n);
				ep.add("~+" + n);
				ep.add("~-" + n);
			}
			((CodeCompletionTextArea) this.jTextArea_filter).setWordList(ep);
		}
		this.jTabbedPane1.add(new SparqlQueryPanel(), "Sparql");
		this.jTabbedPane1.add(new TemplateListPanel(), "Template List");
//		this.jTabbedPane1.add(new TemplateMainView(), "create Template");
		this.jTabbedPane1.add(new TemplateMainView2(), "create Template");
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        resultPanel = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jCheckBox_class = new javax.swing.JCheckBox();
        jCheckBox_instance = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea_filter = new CodeCompletionTextArea();
        jButton1 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();

        jSplitPane1.setName("jSplitPane1"); // NOI18N

        resultPanel.setName("resultPanel"); // NOI18N
        resultPanel.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                resultPanelStateChanged(evt);
            }
        });
        jSplitPane1.setRightComponent(resultPanel);

        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Structure"));
        jPanel2.setName("jPanel2"); // NOI18N

        jCheckBox_class.setSelected(true);
        jCheckBox_class.setText("Classes");
        jCheckBox_class.setName("jCheckBox_class"); // NOI18N
        jCheckBox_class.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_classActionPerformed(evt);
            }
        });

        jCheckBox_instance.setSelected(true);
        jCheckBox_instance.setText("Instances");
        jCheckBox_instance.setName("jCheckBox_instance"); // NOI18N
        jCheckBox_instance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_instanceActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jCheckBox_class)
                .addGap(18, 18, 18)
                .addComponent(jCheckBox_instance)
                .addContainerGap(132, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox_class)
                    .addComponent(jCheckBox_instance))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Filter"));
        jPanel3.setName("jPanel3"); // NOI18N

        jLabel1.setText("+Name will include the name, -Name will Drop all");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Results that contains extact this Name and ~+/~- Name ");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("will use contains instead of equals for search");
        jLabel3.setName("jLabel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea_filter.setColumns(20);
        jTextArea_filter.setRows(5);
        jTextArea_filter.setName("jTextArea_filter"); // NOI18N
        jTextArea_filter.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextArea_filterKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTextArea_filter);

        jButton1.setText("Update");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jTabbedPane1StateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 667, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void jTabbedPane1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jTabbedPane1StateChanged
	((QueryPanel)this.jTabbedPane1.getSelectedComponent()).setFocus();
}//GEN-LAST:event_jTabbedPane1StateChanged

private void jTextArea_filterKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextArea_filterKeyPressed
	QueryManager.filterText = this.jTextArea_filter.getText();
}//GEN-LAST:event_jTextArea_filterKeyPressed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
	TWI.getInstance().repeatQuery();
}//GEN-LAST:event_jButton1ActionPerformed

	private void jCheckBox_classActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jCheckBox_classActionPerformed
		QueryManager.showClasses = this.jCheckBox_class.isSelected();
	}// GEN-LAST:event_jCheckBox_classActionPerformed

	private void jCheckBox_instanceActionPerformed(
			java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jCheckBox_instanceActionPerformed
		QueryManager.showInstance = this.jCheckBox_instance.isSelected();
	}// GEN-LAST:event_jCheckBox_instanceActionPerformed

	private void resultPanelStateChanged(javax.swing.event.ChangeEvent evt) {// GEN-FIRST:event_resultPanelStateChanged
		TWI.getInstance().repeatQuery();
	}// GEN-LAST:event_resultPanelStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox_class;
    private javax.swing.JCheckBox jCheckBox_instance;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea_filter;
    private javax.swing.JTabbedPane resultPanel;
    // End of variables declaration//GEN-END:variables

	public void setResult(QueryResult result) {
		ResultView resView = (ResultView) this.resultPanel
				.getSelectedComponent();
		resView.setResult(result);
		validate();
	}

	private void update() {
		TWI.getInstance().repeatQuery();
	}

}
